def merge_files_and_optimize(input_files, output_file, encoding='utf-8'):
    try:
        with open(output_file, 'w', encoding=encoding) as output:
            for input_file in input_files:
                with open(input_file, 'r', encoding=encoding) as f:
                    lines = f.readlines()
                    lines = [line.strip() for line in lines if line.strip()]
                    output.write('\n'.join(lines) + '\n')
        print("文件合并成功！")
    except Exception as e:
        print("文件合并失败:", str(e))

if __name__ == "__main__":

    # 要合并的文件列表
    # input_files = [
    #     "dataset_wavs/NickYuan/243_train_lists.txt", 
    #     "dataset_wavs/NickYuan/247_train_lists.txt",
    #     "dataset_wavs/NickYuan/248_train_lists.txt",
    #     "dataset_wavs/NickYuan/249_train_lists.txt",
    #     "dataset_wavs/NickYuan/262_train_lists.txt",
    #     "dataset_wavs/NickYuan/264_train_lists.txt",
    #     "dataset_wavs/NickYuan/266_train_lists.txt",

    #     "dataset_wavs/NickYuan/269_train_lists.txt",
    #     "dataset_wavs/NickYuan/271_train_lists.txt",
    #     "dataset_wavs/NickYuan/273_train_lists.txt",
    # ]


    input_files = [
        "dataset_wavs/NickYuan/247_train_lists.txt",
        "dataset_wavs/NickYuan/248_train_lists.txt",
        "dataset_wavs/NickYuan/262_train_lists.txt",
        "dataset_wavs/NickYuan/266_train_lists.txt",
        "dataset_wavs/NickYuan/267_train_lists.txt",
        "dataset_wavs/NickYuan/273_train_lists.txt",
    ]
    output_file = "dataset_wavs/NickYuan/all_train_lists.txt"  # 替换为输出的文件名
    merge_files_and_optimize(input_files, output_file)
